package yja.gzw.app.activity

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.MediaStore
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.Observer
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityJubaoBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import java.io.File

class JubaoActivity :BaseActivity<Mainviewmodel, ActivityJubaoBinding>(R.layout.activity_jubao){
    var file: File?=null
    override fun initView() {

        binding.tijioa.setOnClickListener {
            Toast.makeText(this, "反馈成功", Toast.LENGTH_SHORT).show()
                finish()
        }


        binding.jubaotupian.setOnClickListener {
            openGallery()

        }


    }
    private fun openGallery() {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, GALLERY_REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == GALLERY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            data?.data?.let { uri ->
                file = File(getRealPathFromURI(uri))

                val requestFile = file?.asRequestBody("multipart/form-data".toMediaTypeOrNull())
                val body = MultipartBody.Part.createFormData("file", file?.name,requestFile!!)
                viewmodel.onluying(body)
            }
        }
    }

    private fun getRealPathFromURI(contentUri: Uri): String {
        val projection = arrayOf(MediaStore.Images.Media.DATA)
        val cursor = contentResolver.query(contentUri, projection, null, null, null)
        val column_index = cursor?.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
        cursor?.moveToFirst()
        val result = cursor?.getString(column_index!!)
        cursor?.close()
        return result!!
    }
    companion object {
        private const val GALLERY_REQUEST_CODE = 1001
    }

    override fun dataObservese() {
        viewmodel.onlusc.observe(this, Observer {
            if(it.code==1){

                val shipinglujin= "http://117ai.32.hzgqapp.com"+it.data.url

                UserData.hechengdeshipinglianjie=shipinglujin
                Log.e("视频上传的路径", shipinglujin)

                Glide.with(this)
                    .setDefaultRequestOptions(
                        RequestOptions()
                            .frame(0)
                            .centerCrop()
                    )
                    .load(shipinglujin)
                    .into(binding.jubaotupian)

            }


        })
    }



}